package 考试;

import java.io.*;

public class 题3 {
    /**
     * 创建一个书籍对象，并把它输出到一个文件 book.bat 中，然后从 book.bat 文件中再把该对
     * 象读取出来，在控制台上输出对象的属性信息。要求用一个 java 类实现。
     */
    static class Book implements Serializable {
        private static final long serialVersionUID = 7641633479508351418L;
        public Book(String bookName, String author, Integer pageNumber, Double money) {
            BookName = bookName;
            Author = author;
            PageNumber = pageNumber;
            Money = money;
        }

        public Book() {
        }

        private String BookName;
        private String  Author;//作者
        private Integer PageNumber;
        private Double Money;

        public String getBookName() {
            return BookName;
        }

        public void setBookName(String bookName) {
            BookName = bookName;
        }

        public String getAuthor() {
            return Author;
        }

        public void setAuthor(String author) {
            Author = author;
        }

        public Integer getPageNumber() {
            return PageNumber;
        }

        public void setPageNumber(Integer pageNumber) {
            PageNumber = pageNumber;
        }

        public Double getMoney() {
            return Money;
        }

        public void setMoney(Double money) {
            Money = money;
        }

        @Override
        public String toString() {//覆盖输出
            return "Book{" +
                    "BookName='" + BookName + '\'' +
                    ", Author='" + Author + '\'' +
                    ", PageNumber=" + PageNumber +
                    ", Money=" + Money +
                    '}';
        }
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Book book = new Book("liu", "xinke", 100, 12.34);
        wfitefunciton(book,"D:\\book.bat");
        System.out.println(readfunciton("D:\\book.bat"));
    }

    public static void wfitefunciton(Book book,String str) throws IOException {//干活了
        File f= new File(str);
        if(!f.exists()) f.createNewFile();
        OutputStream fls= new FileOutputStream(f);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fls);
        objectOutputStream.writeObject(book);
    }

    public static Book readfunciton(String str) throws IOException, ClassNotFoundException {
        File f = new File(str);
        FileInputStream fls= new FileInputStream(f);
        ObjectInputStream objectInputStream = new ObjectInputStream(fls);
        return (Book)objectInputStream.readObject();
    }

}

